from django import forms
from django.forms import fields
from django.forms import widgets

from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator

from stu_exam_manage.models import UserInfo     # 导入用户类 UserInfo
from stu_exam_manage.models import College_Class    # 导入班级类 College_Class
from stu_exam_manage.models import Course       # 导入课程类 Course

#登录组件
class UserForm(forms.Form):
    uname = fields.CharField(max_length=20, min_length=1, label='用户名', required=True,widget=widgets.TextInput(attrs={'class':'form-control'}))
    upwd = fields.CharField(max_length=20, min_length=1, label='密码', required=True,widget=widgets.PasswordInput(attrs={'class':'form-control','id':'exampleInputPassword1'}))


#注册组件
class RegisterForm(forms.Form):
    '''
        Warning: 类中用户名为 name, Form组件中用户名为 uname
    '''
    uname = fields.CharField(max_length=20, min_length=1, label='用户名', required=True,widget=widgets.TextInput(attrs={'class':'form-control'}))
    upwd = fields.CharField(max_length=20, min_length=1, label='密   码', required=True,widget=widgets.PasswordInput(attrs={'class':'form-control','id':'exampleInputPassword1'}))
    # ulev = fields.ChoiceField(
    #     choices=((0,'超级用户'),(1,'普通用户'),(2,'待审核用户')),
    #     initial=2,
    #     # widget=widgets.RadioSelect,
    #     label='类别',
    #     widget = widgets.Select(attrs={'readonly':True,'class':'form-control'}),
    #
    # )
    # utel = fields.CharField(max_length=11,min_length=11,label='电话',widget=widgets.NumberInput(attrs={'class':'form-control'}))
    # uaddr = fields.CharField(max_length=32,label='地址',widget=widgets.TextInput(attrs={'class':'form-control'}))
    # uimg = fields.ImageField(label='图片',widget=widgets.FileInput(attrs={'id':'exampleInputFile'}))
    #
    # #图片验证
    # def clean_uimg(self):
    #     value = self.cleaned_data['uimg']
    #     if value.name.split('.')[-1] not in ['jpg','png']:
    #         raise ValidationError('只能录入jpg,png格式的图片')
    #     else:
    #         return value

    # 用户验证
    def clean_uname(self):
        uname = self.cleaned_data['uname']
        print('---',uname)
        if UserInfo.objects.filter(username=uname):
            raise  ValidationError('该用户已经存在')
        else:
            return uname

    # 密码验证
    def clean_upwd(self):
        upwd = self.cleaned_data['upwd']
        if len(upwd) < 6:
            raise  ValidationError('密码不能少于6位')
        else:
            return upwd


class College_ClassForm(forms.Form):
    college_class = College_Class.objects.values('id', 'name')
    college_class_header = (0, '------------请  选  择----------')  # 设置初始班级选择为 ‘---请选择---’
    # college_class_header = []

    name = fields.ChoiceField(
        choices=[(college_class_header), ] + list(College_Class.objects.values_list('id', 'name').order_by('name')),
        # 加入了排序
        widget=widgets.Select(attrs={'class':'form-control'})
    )

    # 实时跟新网页数据
    def __init__(self, *args, **kwargs):
        super(College_ClassForm, self).__init__(*args, **kwargs)
        college_class_header = (0, '------------请  选  择----------')  # 设置初始班级选择为 ‘---请选择---’
        # college_class_header = []
        self.fields['name'].choices = [(college_class_header), ] + list(
            College_Class.objects.values_list('id', 'name').order_by('name'))  # 更新 ， 加入了排序


# 课程form
class CourseForm(forms.Form):
    course = Course.objects.values('id', 'name')
    course_header = (0, '------------请  选  择----------')  # 设置初始班级选择为 ‘---请选择---’
    # college_class_header = []

    name = fields.ChoiceField(
        choices=[(course_header), ] + list(Course.objects.values_list('id', 'name').order_by('name')),
        # 加入了排序
        widget=widgets.Select(attrs={'class':'form-control', 'id': 'course'})
    )

    # 实时跟新网页数据
    def __init__(self, *args, **kwargs):
        super(CourseForm, self).__init__(*args, **kwargs)
        course_header_header = (0, '------------请  选  择----------')  # 设置初始班级选择为 ‘---请选择---’
        # college_class_header = []
        self.fields['name'].choices = [(course_header_header), ] + list(
            Course.objects.values_list('id', 'name').order_by('name'))  # 更新 ， 加入了排序


